using System;
using System.Collections.Generic;
using System.Text;
using SlimDX;
using System.Xml;
using Barrage.Global;

namespace Barrage.GUI.Elements
{
    /// <summary>
    /// Event that fires off when a checkbox is clicked
    /// </summary>
    /// <param name="buttonId">GUIElement id</param>
    /// <param name="value">Current checkbox value</param>
    public delegate void GUICheckboxHandler(string buttonId,bool value);
    /// <summary>
    /// GUIElement Checkbox item with two values (checked and unchecked)
    /// </summary>
    public class GUICheckbox : GUISelectableElement
    {
        const string XML_WIDTH_ATTRIBUTE = "width";
        const string XML_HEIGHT_ATTRIBUTE = "height";
        const string XML_TEXT_ATTRIBUTE = "text";

        /// <summary>
        /// Fires off when the checkbox changes its value
        /// </summary>
        public event GUICheckboxHandler OnInteraction;

        Vector2 size;
        /// <summary>
        /// Size for mouse pointer interaction
        /// </summary>
        public Vector2 Size { get { return size; } }

        bool isChecked = false;
        /// <summary>
        /// Gets / Sets the checkbox state
        /// </summary>
        public bool Checked { get { return isChecked; } set { isChecked = value; } }

        bool hovering = false;
        /// <summary>
        /// Returns true if the mouse's hovering over the item
        /// </summary>
        public bool Hovering { get { return hovering; } }

        string text;
        /// <summary>
        /// Gets / Sets the checkbox's text
        /// </summary>
        public virtual string Text { get { return text; } set { text = value; } }

        /// <summary>
        /// Default item size. Can be overriden
        /// </summary>
        protected virtual Vector2 DefaultSize { get { return new Vector2(100, 100); } }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="layout">Layout that will hold this item</param>
        /// <param name="template">Template that will be used to render this item</param>
        /// <param name="xn">Xml Node that holds attribute values of this item</param>
        public GUICheckbox(GUILayout layout, string template, XmlNode xn)
            : base(layout, template, xn)
        {
            size = GameMath.ParseVector2(XmlHelper.GetXMLAttribute(xn, XML_WIDTH_ATTRIBUTE), XmlHelper.GetXMLAttribute(xn, XML_HEIGHT_ATTRIBUTE), DefaultSize);
            text = Language.LanguageManager.Texts[XmlHelper.GetXMLAttribute(xn, XML_TEXT_ATTRIBUTE)];
        }

        /// <summary>
        /// \internal Updates matrices and item logic
        /// </summary>
        protected override void DoUpdate()
        {
            base.DoUpdate();
            hovering = CheckIntersection();
            if (hovering) Layout.SelectedItemIndex = Order;
            CheckClick();

        }

        void CheckClick()
        {
            if (Mouse.Instance.ButtonReleased() && hovering)
                Interact();
        }

        bool CheckIntersection()
        {
            Vector3 mPos = new Vector3(Mouse.Instance.Position, 0);
            Vector4 tPos = GameMath.VectorAbs(Vector3.Transform(mPos, Matrix.Invert(motion.WorldMatrix)));

            return ((tPos.X <= size.X / 2f) && (tPos.Y <= size.Y / 2f));
        }

        /// <summary>
        /// \internal Fires OnInteraction event
        /// </summary>
        public override void Interact()
        {
            isChecked = !isChecked;
            if (this.OnInteraction != null)
                this.OnInteraction.Invoke(Id, isChecked);
        }
    }

}
